<?php

namespace common\behavior\CommonMessagePushing;


use common\model\OrderModel;
use common\service\BehaviorService;
use common\service\OrderService;
use common\service\TaskService;
use Webman\Stomp\Client;

/**
 * 订单审核结果 消息通知
 * 通知乙方确认订单
 * update @ 2023年1月14日 乙方此时还可能没有项目 所以查询租户里的商务制片
 */
class OrderReviewResultOutsourceBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'order_id' => 'intval',
            ],
            [
                'order_id' => 0,
            ],
            'data',
            ['type' => 'order_review_result_outsource'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($orderId) = $params;
        if (empty($orderId)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $order = model(OrderModel::class)->find($orderId);
        $users = teamones_request('teamones-im', 'tenant/select_department_user', [
            'param' => [
                'filter' => [
                    'tenant_id' => $order['to_tenant_id'],
                    'role_code' => TaskService::BUSINESS_PRODUCER_CODE,
                ],
            ],
        ])['data'];
        $tenantPartyA = teamones_request('teamones-im', 'tenant/find', [
            'param' => [
                'filter' => [
                    'id' => $order['from_tenant_id']
                ]
            ]
        ])['data'];
        $messageParam = [
            'first' => '有新的订单',
            'customer_name' => $tenantPartyA['name'],
            'order_name' => $order['name'],
            'create_at' => date("Y/m/d H:i"),
            'remark' => '请及时处理',
            "tenant_id" => $order['from_tenant_id'],
            "project_id" => $order['project_id'],
            "order_type" => $order['type'],
            "order_id" => $order['id'],
        ];
        $messageTemplateId = OrderService::getInstance()->getNotifyPartyBToAcceptOrderTemplateId();
        $messageGenerated = [];
        foreach ($users['rows'] as $user) {
            $messageGenerated[] = [
                "param" => $messageParam,
                'receiver' => $user['user_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'message_key' => 'notify_party_b_to_accept_order'
            ];
        }

        /***********  发送新的队列  ************/
        foreach ($messageGenerated as $message) {
            Client::send('common_message_pushing', build_queue_data_with_xu([
                'message_generated' => [$message],
            ]));
        }
        /***********  结束  ************/
    }
}
